package org.linlinjava.litemall.db.util;

import java.text.MessageFormat;
import java.util.Date;

import org.linlinjava.litemall.db.service.LitemallSequenceService;

public class SequenceUtils {
	public static final String SUPPLY_BILL_SEWUENCE_CODE = "supply_bill_sewuence_code";
	public static final String PURCHASE_BILL_SEWUENCE_CODE = "purchase_bill_sewuence_code";

	public static final int CODE_LENGTH_5 = 5;
	public static final int CODE_LENGTH_4 = 4;
	public static final int CODE_LENGTH_3 = 3;

	private static LitemallSequenceService sequenceService;

	public static LitemallSequenceService getLitemallSequenceService() {
		if (sequenceService == null) {
			sequenceService = SpringContextUtils.getBean(LitemallSequenceService.class);
		}
		return sequenceService;
	}

	/**
	 * 
	 * @param sequenceCodeFormat
	 * @param codeType
	 * @param dateFormat
	 * @param codeLength
	 * @return
	 */
	public static String getNextValue(String sequenceCodeFormat, String codeType, String dateFormat,
			Integer codeLength) {
		String todayStr = DateUtils.format(new Date(), dateFormat);
		String sequenceValue = getLitemallSequenceService().getNextValue(codeLength,
				MessageFormat.format("{0}:{1}", sequenceCodeFormat, codeType), todayStr);
		return MessageFormat.format("{0}{1}{2}", codeType, todayStr, sequenceValue);
	}

	/**
	 * 获取供应申请单据号码
	 * 
	 * @return
	 */
	public static String getSupplySequenceCode() {
		return getNextValue(SUPPLY_BILL_SEWUENCE_CODE, "", DateUtils.YYYYMMDD, CODE_LENGTH_5);
	}

	/**
	 * 获取采购申请单据号码
	 * 
	 * @return
	 */
	public static String getPurchaseSequenceCode() {
		return getNextValue(PURCHASE_BILL_SEWUENCE_CODE, "", DateUtils.YYYYMMDD, CODE_LENGTH_5);
	}
}
